package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.text.ParseException;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import util.Mascaras;

public class Cadastros extends JPanel{

    private JPanel jpUsuarios, jpMedicos;
    private AreaUsuarios areaUsuarios;
    private AreaMedicos areaMedicos;
    
    public Cadastros() {
        super();
        //Instânciar objetos
        jpUsuarios = new JPanel(new BorderLayout());
        jpMedicos = new JPanel(new BorderLayout());
        areaUsuarios = new AreaUsuarios();
        areaMedicos = new AreaMedicos();
        
        //definir propriedades visuais
        jpUsuarios.setBorder(javax.swing.BorderFactory.createTitledBorder("Usuários"));
        jpMedicos.setBorder(javax.swing.BorderFactory.createTitledBorder("Médicos"));
        
        //definir layouts
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        
        //definir listeners
        
        //adicionar objetos a conteineres        
        jpUsuarios.add(areaUsuarios, BorderLayout.CENTER);
        jpMedicos.add(areaMedicos, BorderLayout.CENTER);
        add(jpUsuarios);
        add(jpMedicos);
        
        //setar propriedades da tela e mostra-la
        setVisible(true);

    }

    private static class AreaEdicaoUsuarios extends JPanel{

        private JTextField jtfLogin;
        private JPasswordField jpfSenha;
        private JLabel jlTipoUsuario;
        private JComboBox jcbTipoUsuario;
        private final String[] tiposUsuarios = {"Médico (a)", "Secretária (o)"};
        private JPanel j1;
        
        public AreaEdicaoUsuarios() {
            super();

            //Instanciar objetos
            jtfLogin = new JTextField();
            jpfSenha = new JPasswordField();
            jlTipoUsuario = new JLabel("Tipo de usuário:", SwingConstants.RIGHT);
            jcbTipoUsuario = new JComboBox(tiposUsuarios);
            j1 = new JPanel(new BorderLayout(5, 0));
            
            //definir propriedades visuais
            jtfLogin.setBorder(javax.swing.BorderFactory.createTitledBorder("Login"));
            jpfSenha.setBorder(javax.swing.BorderFactory.createTitledBorder("Senha"));
            
            //definir layouts
            setLayout(new GridLayout(1, 3, 5, 10));

            //definir listeners

            //adicionar objetos a conteineres
            j1.add(jlTipoUsuario, BorderLayout.WEST);
            j1.add(jcbTipoUsuario, BorderLayout.CENTER);
            add(jtfLogin);
            add(jpfSenha);
            add(j1);
            
            //setar propriedades da tela e mostra-la
            setVisible(true);

        }
    }

    private static class AreaEdicaoMedicos extends JPanel{

        private JTextField jtfNome, jtfCRM, jtfEmail;
        private JFormattedTextField jftfCPF, jftfTelefone, jftfCelular;
        private JPanel j1;
        
        public AreaEdicaoMedicos() {
            super();

            //Instanciar objetos
            jtfNome = new JTextField();
            jtfCRM = new JTextField();
            jtfEmail = new JTextField();
            jftfCPF = new JFormattedTextField();
            jftfTelefone = new JFormattedTextField();
            jftfCelular = new JFormattedTextField();
            j1 = new JPanel(new GridLayout(1, 5, 5, 1));
            
            formatarCampos();
            
            //definir propriedades visuais
            jtfNome.setBorder(javax.swing.BorderFactory.createTitledBorder("Nome"));
            jtfCRM.setBorder(javax.swing.BorderFactory.createTitledBorder("CRM"));
            jtfEmail.setBorder(javax.swing.BorderFactory.createTitledBorder("E-mail"));
            jftfCPF.setBorder(javax.swing.BorderFactory.createTitledBorder("CPF"));
            jftfTelefone.setBorder(javax.swing.BorderFactory.createTitledBorder("Telefone"));
            jftfCelular.setBorder(javax.swing.BorderFactory.createTitledBorder("Celular"));
            
            //definir layouts
            setLayout(new GridLayout(2, 1, 5, 10));

            //definir listeners

            //adicionar objetos a conteineres
            j1.add(jtfCRM);
            j1.add(jftfCPF);
            j1.add(jftfTelefone);
            j1.add(jftfCelular);
            j1.add(jtfEmail);
            add(jtfNome);
            add(j1);
            
            //setar propriedades da tela e mostra-la
            setVisible(true);

        }
        
        private void formatarCampos() {
            try {
                Mascaras.maskCPF(jftfCPF);
                Mascaras.maskTelefone(jftfTelefone);
                Mascaras.maskTelefone(jftfCelular);
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
        }
    }

    private class AreaUsuarios extends JPanel{

        private JPanel areaBotoes, areaDados;
        private AreaEdicaoUsuarios areaEdicao;
        private JButton jbIncluir, jbAlterar, jbExcluir, jbPesquisar;
        
        public AreaUsuarios() {
            super();

            //Instanciar objetos
            areaBotoes = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
            areaDados = new JPanel(new BorderLayout());
            jbIncluir = new JButton("Incluir");
            jbAlterar = new JButton("Alterar");
            jbExcluir = new JButton("Excluir");
            jbPesquisar = new JButton("Pesquisar");
            areaEdicao = new AreaEdicaoUsuarios();
            
            //definir propriedades visuais

            //definir layouts
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            areaBotoes.setLayout(new GridLayout(1, 7, 10, 5));

            //definir listeners

            //adicionar objetos a conteineres
            areaBotoes.add(new JPanel());
            areaBotoes.add(jbIncluir);
            areaBotoes.add(jbAlterar);
            areaBotoes.add(jbExcluir);
            areaBotoes.add(jbPesquisar);
            areaBotoes.add(new JPanel());
            add(areaEdicao);
            add(util.ElementoVazio.gerar(0, 5));
            add(areaBotoes);
            add(util.ElementoVazio.gerar(0, 5));
            add(areaDados);

            //setar propriedades da tela e mostra-la
            setVisible(true);
        }
    }

    private class AreaMedicos extends JPanel{

        private JPanel areaBotoes, areaDados;
        private AreaEdicaoMedicos areaEdicao;
        private JButton jbIncluir, jbAlterar, jbExcluir, jbPesquisar;
        
        public AreaMedicos() {
            super();
            
            //Instanciar objetos
            areaBotoes = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
            areaDados = new JPanel(new BorderLayout());
            jbIncluir = new JButton("Incluir");
            jbAlterar = new JButton("Alterar");
            jbExcluir = new JButton("Excluir");
            jbPesquisar = new JButton("Pesquisar");
            areaEdicao = new AreaEdicaoMedicos();
            
            //definir propriedades visuais

            //definir layouts
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            areaBotoes.setLayout(new GridLayout(1, 7, 10, 5));

            //definir listeners

            //adicionar objetos a conteineres
            areaBotoes.add(new JPanel());
            areaBotoes.add(jbIncluir);
            areaBotoes.add(jbAlterar);
            areaBotoes.add(jbExcluir);
            areaBotoes.add(jbPesquisar);
            areaBotoes.add(new JPanel());
            add(areaEdicao);
            add(util.ElementoVazio.gerar(0, 5));
            add(areaBotoes);
            add(util.ElementoVazio.gerar(0, 5));
            add(areaDados);

            //setar propriedades da tela e mostra-la
            setVisible(true);
        }
    }
}
